-- @owner: @zhanghuan96
-- @date: 2024/12/18
-- @testpoint: gms_utility.format_error_stack函数，测试堆栈深度
@conn PrimaryDbAmode;
--前置条件
drop extension if exists gms_utility;
create extension gms_utility;
--step1:创建自定义函数;expect:成功
drop function if exists func_format0007_01;
create or replace function func_format0007_01(a int, b int)
returns int as $$
declare
    res int := 0;
begin
    res = a / b;
    return res;
exception
    when others then
    raise exception 'expected exception';
end;
$$ language plpgsql;
/
--step2:创建自定义函数，函数嵌套上述函数;expect:成功
drop function if exists func_format0007_02;
create or replace function func_format0007_02(a int, b int)
returns int as $$
declare
    res int := 0;
begin
    res := func_format0007_01(a, b);
    return res;
exception
    when others then
    RAISE INFO 'INFO:%', gms_utility.format_error_stack();
    return -1;
end;
$$ language plpgsql;
/
--step3:创建存储过程;expect:成功
drop procedure if exists func_format0007_03;
create or replace procedure func_format0007_03(count in out int)
as
begin
    if count < 1000 then
        count := count + 1;
        func_format0007_03(count);
    else
        func_format0007_01(100, 0);
    end if;
exception
    when others then
    RAISE INFO 'INFO:%', gms_utility.format_error_stack();
end;
/
--step4:执行存储过程;expect:成功
declare
    c int := 0;
begin
    func_format0007_03(c);
end;
/
--step5:清理环境;expect:成功
drop function if exists func_format0007_01;
drop function if exists func_format0007_02;
drop procedure if exists func_format0007_03;
drop extension if exists gms_utility;